import { BaseEntity } from "../entity/BaseEntity";
import { EntityState, EntityType } from "./GameConst";

export class EntityVo {
    public id:number = 0;
    public type: EntityType = 0;
    protected _state: EntityState;
    protected _entity: BaseEntity;
    //#region protected
    protected init() {}
    protected playIdle() { }
    protected playWalk() { }
    //#endregion

    public initVo(data?: any) {
        if (data) {
            for (let key in data) {
                this[key] = data[key];
            }
        }
        this._state = EntityState.idle;
        this.init();
    }

    public setEntity(entity: BaseEntity) {
        this._entity = entity;
    }

    public get entity():BaseEntity{
        return this._entity;
    }

    public setState(state: EntityState) {
        if (this._state == state) return false;
        if (!this.checkState(state)) return false;
        
        switch (state) {
            case EntityState.idle:
                this.onIdle();
                break;
            case EntityState.walk:
                this.onWalk();
                break;
        }
        this.onStateChanged(state);
        this._state = state;
        return true;
    }

    private onStateChanged(state: EntityState) {
        if (this._entity) this._entity.onStateChanged(state);
    }

    private checkState(state: EntityState): boolean {
        switch (state) {
            case EntityState.idle:
                break;
            case EntityState.walk:
                break;
        }
        return true;
    }

    private onIdle() {
        this.playIdle();
    };

    private onWalk() {
        this.playWalk();
    };

    public isMoving() {
        return this._state == EntityState.walk;
    }
    
    public clear(){
    }

    public get state(): number {
        return this._state;
    }
    
    public get entityId():string {
        return this.type + "_" + this.id;
    }
}